package com.carbonspark.ecoquest.domain.service.impl;

import com.carbonspark.ecoquest.common.enums.IsAbandonedEnum;
import com.carbonspark.ecoquest.common.enums.IsCompletedEnum;
import com.carbonspark.ecoquest.domain.convert.TaskUserBOConverter;
import com.carbonspark.ecoquest.domain.entity.TaskUserBO;
import com.carbonspark.ecoquest.domain.redis.RedisUtil;
import com.carbonspark.ecoquest.domain.service.TaskUserDomainService;
import com.carbonspark.ecoquest.infra.basic.entity.TaskRules;
import com.carbonspark.ecoquest.infra.basic.entity.TaskUser;
import com.carbonspark.ecoquest.infra.basic.service.TaskRulesService;
import com.carbonspark.ecoquest.infra.basic.service.TaskUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class TaskUserDomainServiceImpl implements TaskUserDomainService {
    @Resource
    private TaskUserService taskUserService;
    @Resource
    private TaskRulesService taskRulesService;
    @Resource
    private RedisUtil redisUtil;
    private final String CLOCK = "clock";
    //完成常量
    private final Integer IS_COMPLETED_YES = 1;
    @Override
    public Boolean insertTaskUser(Long id, Long taskId) {
        TaskUser taskBO = taskUserService.queryTask(id,taskId);
        if (taskBO!=null){
            return false;
        }
        TaskUser taskUser = new TaskUser();
        taskUser.setTaskId(taskId);
        taskUser.setUserId(id);
        taskUser.setDays(0);
        taskUser.setIsAbandoned(IsAbandonedEnum.UN_DELETED.code);
        taskUser.setIsCompleted(IsCompletedEnum.UN_DELETED.code);
        return   taskUserService.insert(taskUser);
    }

    @Override
    public Boolean queryByTask(Long id, Long taskId) {
        TaskUser task = taskUserService.queryTask(id,taskId);
        if (task!=null){
            return false;
        }
        return true;
    }

    @Override
    public Boolean delete(Long id, Long taskId) {
       Boolean result = taskUserService.deleteByIdTAskId(id,taskId);
        return result;
    }

    @Override
    public TaskUserBO queryDays(Long id, Long taskId) {
        TaskUser task = taskUserService.queryTask(id,taskId);
        TaskUserBO taskUserBO = TaskUserBOConverter.INSTANCE.convertEntityToBO(task);
        return taskUserBO;
    }

    @Override
    public TaskUserBO queryIsCompleted(Long userId, Long taskId) {
        TaskUser taskBO = taskUserService.queryTask(userId, taskId);
        TaskUserBO taskUserBO = TaskUserBOConverter.INSTANCE.convertEntityToBO(taskBO);
        return taskUserBO;
    }

    @Override
    @Transactional
    public Boolean clock(Long id, Long taskId) {
        String key = redisUtil.buildKey(CLOCK, id.toString(), taskId.toString());
        boolean result = redisUtil.setNx(key, "1", 24L, TimeUnit.HOURS);
        if (!result) {
            return false;
        }

        try {
            // 打卡加1
            taskUserService.updateDays(id, taskId);

            // 查询打卡完后更新的数据
            TaskUser taskBO = taskUserService.queryTask(id, taskId);

            // 查询打卡规则
            Integer count = taskRulesService.queryRulesByTaskId(taskId);

            // 如果已经等于则代表完成任务，将状态改成完成
            if (count.equals(taskBO.getDays())) {
                taskBO.setIsCompleted(IS_COMPLETED_YES);
                taskUserService.update(taskBO);
            }

            return true;
        } catch (Exception e) {
            // 如果业务逻辑执行失败，删除 Redis 中的 key
            redisUtil.del(key);
            throw e; // 抛出异常，触发事务回滚
        }
    }
}
